﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using dal;

/// <summary>
/// 近期关注板块业务层
/// </summary>
public class GzbkBLL
{
    public string GetData()
    {
        string key = "GzbkBLL_GetData_fasdfj";
        string str = (string)MemcachedTools.Get(key);
        if (!string.IsNullOrEmpty(str))
        {
            return str;
        }
        CMSEntities cms = new CMSEntities();
        var tid = cms.TopicsType.First(x => x.ID == "jqgzbk").NID;
        var content = cms.Topics.First(x => x.ID == tid).Content;
        var data= Newtonsoft.Json.JsonConvert.DeserializeObject<List<Gzbk>>(content);
        if (data == null)
            return string.Empty;
        NewsListBLL bll = new NewsListBLL();
        for (int i = 0; i < data.Count; i++)
        {
            var c = data[i];
            string[] keywords = new string[] { c.BKName };
            var lst= bll.GetListByKeyWord(keywords, 5);
            c.NewsLst = new List<Gzbk_News>();
            for (int j = 0; j < lst.Count; j++)
            {
                c.NewsLst.Add(new Gzbk_News() { Id=lst[j].Id, Title=lst[j].Title });
            }
            if(data[i].Stocks!=null)
            {
                for (int j = 0; j < data[i].Stocks.Count; j++)
                {
                    var t= bll.GetListByStock(data[i].Stocks[j].Code,1);
                    if (t.Count > 0)
                    {
                        data[i].Stocks[j].News = new Gzbk_News();
                        data[i].Stocks[j].News.Id = t[0].Id;
                        data[i].Stocks[j].News.Title = t[0].Title;
                    }
                }
            }
        }
        str = Newtonsoft.Json.JsonConvert.SerializeObject(data);
        MemcachedTools.Add(key, str, DateTime.Now.AddHours(1));
        return str;
    }
}